package com.zx.flow.api.execute.response;

import com.zx.flow.api.dict.FlowDict.ExecuteStatus;
import com.zx.flow.api.execute.model.FlowContext;
import java.util.Objects;
import lombok.Data;

@Data
public class FlowResult {

    private ExecuteStatus status;
    private String msg;
    private FlowContext flowContext;

    public static FlowResult ok(FlowContext flow) {
        return ok("", flow);
    }

    public static FlowResult ok() {
        return ok("", null);
    }

    public static FlowResult ok(String msg, FlowContext flow) {
        FlowResult result = new FlowResult();
        result.setStatus(ExecuteStatus.success);
        result.setMsg(msg);
        result.setFlowContext(flow);
        return result;
    }

    public static FlowResult exception(String msg, FlowContext flow) {
        FlowResult result = new FlowResult();
        result.setStatus(ExecuteStatus.exception);
        result.setMsg(msg);
        result.setFlowContext(flow);
        return result;
    }

    public static FlowResult fail(String msg, FlowContext flow) {
        FlowResult result = new FlowResult();
        result.setStatus(ExecuteStatus.failed);
        result.setMsg(msg);
        result.setFlowContext(flow);
        return result;
    }

    public static FlowResult invalid(String msg, FlowContext flow) {
        FlowResult result = new FlowResult();
        result.setStatus(ExecuteStatus.invalid);
        result.setMsg(msg);
        result.setFlowContext(flow);
        return result;
    }

    public static FlowResult fail(String msg) {
        return fail(msg, null);
    }

    public boolean isOk() {
        return Objects.equals(this.status, ExecuteStatus.success)
                || Objects.equals(this.status, ExecuteStatus.invalid);
    }

    public boolean isException() {
        return Objects.equals(this.status, ExecuteStatus.exception);
    }

    public boolean isFailed() {
        return Objects.equals(this.status, ExecuteStatus.failed);
    }

    @Override
    public String toString() {
        return "[" + "status=" + status + ", msg=" + msg +
                ", flowId=" + (flowContext == null ? "null" : flowContext.getId()) +
                ']';
    }
}
